﻿using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using ESRI.ArcGIS.Client.Async;

namespace ArcGIS.Client.Async.Tests
{
    //[TestClass]
    public class EnumExtensionMethods
    {
        //[TestMethod]
        public void EnumMethods()
        {
            var assembly = typeof(ArcGISDynamicMapServiceLayerExtensions).Assembly;
            var extensionMethods = GetExtensionMethods(assembly)
                .ToList();

            var sb = new StringBuilder();
            var namespaces = extensionMethods
                .Select(x => x.DeclaringType.Namespace)
                .OrderBy(x => x)
                .Distinct();

            foreach (var ns in namespaces)
            {
                sb.AppendLine(string.Format("* *{0}*", ns));
                var methodsByType = extensionMethods.Where(x => x.DeclaringType.Namespace == ns)
                    .ToLookup(x => x.GetParameters()[0].ParameterType);

                foreach (var key in methodsByType.OrderBy(x => x.Key.Name).Select(x => x.Key))
                {
                    sb.AppendLine(string.Format("** {0}", key.Name));
                    var methods = methodsByType[key];
                    foreach (var methodName in methods.Select(x => x.Name).OrderBy(x => x).Distinct())
                    {
                        sb.AppendLine(string.Format("*** _{0}_", methodName));                        
                    }
                }

            }

            var formattedString = sb.ToString();
            Debugger.Break();
        }

        private static IEnumerable<MethodInfo> GetExtensionMethods(Assembly assembly)
        {
            return from type in assembly.GetTypes()
                   where type.IsSealed && !type.IsGenericType && !type.IsNested && type.IsPublic
                   from method in type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
                   where method.IsDefined(typeof(ExtensionAttribute), false)
                   select method;
        }
    }
}
